package com.cx.service.wjr.impl;

import com.cx.mapper.PageDeliveryMapper;
import com.cx.mapper.PageMapper;
import com.cx.mapper.UserMapper;
import com.cx.pojo.*;
import com.cx.pojo.wjr.UserPage;
import com.cx.service.wjr.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminUserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Resource
    PageMapper pageMapper;
    @Resource
    PageDeliveryMapper pageDeliveryMapper;

    @Transactional(propagation =  Propagation.REQUIRED)
    public int addUserPage(UserPage userPage){
        System.out.println("impl执行了");
        int  pageNum = 100000000+(int)(Math.random()*(900000000+100000000-100000000));
        int  pageCode = 100000+(int)(Math.random()*(900000+100000-100000));
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;
        boolean flag = false;
        for (int i = 0; i < 10; i++) {
            try{
                userPage.setPageNum(pageNum);
                userPage.setPageCode(String.valueOf(pageCode));
                PageDelivery pageDelivery = new PageDelivery();
                pageDelivery.setStartProvince(userPage.getSendProvince());
                pageDelivery.setStartCity(userPage.getSendCity());
                pageDelivery.setStartArea(userPage.getSendArea());
                pageDelivery.setNowProvince(userPage.getSendProvince());
                pageDelivery.setNowArea(userPage.getSendArea());
                pageDelivery.setNowCity(userPage.getSendCity());
                pageDelivery.setEndProvince(userPage.getReceiveProvince());
                pageDelivery.setEndArea(userPage.getReceiveArea());
                pageDelivery.setEndCity(userPage.getReceiveCity());
                pageDelivery.setPageNum(userPage.getPageNum());
                count1 =  pageMapper.insertUserPageSend(userPage);
                count2 = pageMapper.insertUserPageReceive(userPage);
                count3 = pageMapper.insertUserPagePage(userPage);
                count4 = pageDeliveryMapper.insert(pageDelivery);
                flag = true;
                break;
            }catch (Exception e){
                pageNum = 100000+(int)(Math.random()*(900000+100000-100000));
                pageCode = 100000+(int)(Math.random()*(900000+100000-100000));
                System.out.println(pageNum);
                System.out.println(pageCode);
            }

        }
        if(!flag){
           throw new RuntimeException("重复次数过多，请检查代码或数据库是否出现异常");
        }
        if(count1+count2+count3+count4<4){
            throw new RuntimeException("插入存在异常，请检查");
        }
        return count1+count2+count3+count4;
    }

    @Override
    public List<Page> selectByPageNum(PageExample pageExample) {
        return pageMapper.selectByExample(pageExample);
    }

    @Override
    public List<Page> selectByPagePhone(PageExample pageExample) {
        return null;
    }

    public List<User> select(UserExample user) {
        return userMapper.selectByExample(user);
    }

    public PageInfo<User> listPage(UserExample b, Integer pageNum, Integer pageSize) { return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> { userMapper.selectByExample(b); }); }


    public User selectByName(User user) {
        return null;
    }

    public int addCourier(User user) {
        return userMapper.insert(user);
    }

    //public PageInfo<UserExample> listPage(UserExample user, int pageNum, int pageSize) { return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> { userMapper.selectByExample(user); }); }


    @Override
    public List<User> selectByExample(UserExample userExample) {
        return userMapper.selectByExample(userExample);
    }

    @Override
    public Integer selectByPageExample() {
//        pageMapper.selectByExample(pageExample);
        return 0;
    }

    @Override
    public int delete(UserExample userExample) {
        return userMapper.deleteByExample(userExample);
    }

    @Override
    public int update(@Param("record") User record, @Param("example") UserExample example) {
        return userMapper.updateByExampleSelective(record,example);
    }
}
